<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Pegawai
 *
 * @author DENY PRASETIA TW
 */
class Pegawai extends CI_Model {

    function __construct() {
        parent::__construct();
    }
    
    #Function untuk melakukan edit data pegawai
    #status : ok
    function edit($input) {
        $this->db->trans_start(TRUE);
            $list_input = array(
                'IDPEGAWAI'     => $input['id_pegawai'],
                'NAMAPEGAWAI'   => $input['nama_pegawai'],
                'PASSWORD'      => $input['password'],
                'HAKAKSES'      => $input['hak_akses'],
                'STATUSPEGAWAI' => $input['status_pegawai'],
                'STATUSLOGIN'   => '0'
            );
            $this->db->where('IDPEGAWAI', $input['id_pegawai']);
            $this->db->set($list_input);
            $this->db->update('T_PEGAWAI');
        $this->db->trans_complete();
        if($this->db->trans_status() === TRUE){
            $this->db->trans_commit();
        }else{
            $this->db->trans_rollback();
            show_error("Maaf telah terjadi kesalahan, data tidak dapat diproses.");
        }
    }
    
    #Function untuk melihat detail data pegawai
    #status : ok
    function detail($input) {
        $this->db->trans_start(TRUE);
            $this->db->select('IDPEGAWAI, NAMAPEGAWAI, PASSWORD, HAKAKSES, STATUSPEGAWAI');
            $this->db->from('T_PEGAWAI');
            $this->db->where('IDPEGAWAI', $input['id_pegawai']);
            $data = $this->db->get();
        $this->db->trans_complete();
        if($this->db->trans_status() === TRUE){
            $this->db->trans_commit();
            return $data->result();
//            foreach($data->result() as $row){
//                echo $row->IDPEGAWAI;
//                echo $row->NAMAPEGAWAI;
//                echo $row->PASSWORD;
//                echo $row->HAKAKSES;
//                echo $row->STATUSPEGAWAI;
//                echo "</br>";
//            }
        }else{
            $this->db->trans_rollback();
            show_error("Maaf telah terjadi kesalahan, data tidak dapat diproses.");
        }
    }
    
    #Function untuk melakukan insert data pegawai
    #status : ok
    function insert($input) {
        $this->db->trans_start(TRUE);
            $list_input = array(
                'IDPEGAWAI'     => $input['id_pegawai'],
                'NAMAPEGAWAI'   => $input['nama_pegawai'],
                'PASSWORD'      => $input['password'],
                'HAKAKSES'      => $input['hak_akses'],
                'STATUSPEGAWAI' => '1',
                'STATUSLOGIN'   => '0'
            );
            $this->db->insert('T_PEGAWAI', $list_input);
        $this->db->trans_complete();
        if($this->db->trans_status() === TRUE){
            $this->db->trans_commit();
        }else{
            $this->db->trans_rollback();
            show_error("Maaf telah terjadi kesalahan, data tidak dapat diproses.");
        }
    }
    
    #Function untuk melakukan menampilkan dan mencari data pegawai
    #status : ok
    function search($input) {
        if($input['nama_pegawai'] == NULL){
            $this->db->trans_start(TRUE);
                $this->db->select('IDPEGAWAI, NAMAPEGAWAI, PASSWORD, HAKAKSES, STATUSPEGAWAI');
                $this->db->from('T_PEGAWAI');
                $this->db->limit($input['limit'], $input['start']);
                $this->db->order_by('NAMAPEGAWAI','ASC');
                $data = $this->db->get();
            $this->db->trans_complete();
            if($this->db->trans_status() === TRUE){
                $this->db->trans_commit();
                return $data->result();
//                foreach($data->result() as $row){
//                    echo $row->IDPEGAWAI;
//                    echo $row->NAMAPEGAWAI;
//                    echo $row->PASSWORD;
//                    echo $row->HAKAKSES;
//                    echo $row->STATUSPEGAWAI;
//                    echo "</br>";
//                }
            }else{
                 $this->db->trans_rollback();
                 show_error("Maaf telah terjadi kesalahan, data tidak dapat diproses.");
            }
        }else{
            $this->db->trans_start(TRUE);
                $this->db->select('IDPEGAWAI, NAMAPEGAWAI, PASSWORD, HAKAKSES, STATUSPEGAWAI');
                $this->db->from('T_PEGAWAI');
                $this->db->like('NAMAPEGAWAI', $input['nama_pegawai']);
                $this->db->or_like('IDPEGAWAI', $input['nama_pegawai']);
                $this->db->limit($input['limit'], $input['start']);
                $data = $this->db->get();
            $this->db->trans_complete();
            if($this->db->trans_status() === TRUE){
                $this->db->trans_commit();
                return $data->result();
//                foreach($data->result() as $row){
//                    echo $row->IDPEGAWAI;
//                    echo $row->NAMAPEGAWAI;
//                    echo $row->PASSWORD;
//                    echo $row->HAKAKSES;
//                    echo $row->STATUSPEGAWAI;
//                    echo "</br>";
//                }
            }else{
                 $this->db->trans_rollback();
                 show_error("Maaf telah terjadi kesalahan, data tidak dapat diproses.");
            }
        }
    }

    #Function untuk menghitung jumlah data pegawai
    #status : ok
    function count_all() {
        $this->db->trans_begin();
            $this->query = $this->db->count_all_results('T_PEGAWAI');
        $this->db->trans_complete();
        if ($this->db->trans_status() === TRUE) {
            $this->db->trans_commit();
            return $this->query;
            //echo $this->query;
        }else{
            $this->db->trans_rollback();
            show_error("Maaf telah terjadi kesalahan, data tidak dapat diproses.");
        }
    }
    
    #Function untuk menghitung jumlah data pegawai waktu search data
    #status : ok
    function count_search($input) {
        $this->db->trans_begin();
            $this->db->from('T_PEGAWAI');
            $this->db->like('NAMAPEGAWAI', $input);
            $this->query = $this->db->count_all_results();
        $this->db->trans_complete();
        if ($this->db->trans_status() === TRUE) {
            $this->db->trans_commit();
            return $this->query;
            //echo $this->query;
        }else{
            $this->db->trans_rollback();
            show_error("Maaf telah terjadi kesalahan, data tidak dapat diproses.");
        }
    }
    
}
